class UserModel {
  final String id;
  final String username;
  final String email;
  final String? phone;
  final String? avatar;
  final String? location;
  final String? bio;
  final List<SkillTag> skills;
  final List<Certification> certifications;
  final double rating;
  final int reviewCount;
  final int followersCount;
  final int followingCount;
  final bool isOnline;
  final DateTime createdAt;
  final DateTime? lastActiveAt;

  UserModel({
    required this.id,
    required this.username,
    required this.email,
    this.phone,
    this.avatar,
    this.location,
    this.bio,
    this.skills = const [],
    this.certifications = const [],
    this.rating = 0.0,
    this.reviewCount = 0,
    this.followersCount = 0,
    this.followingCount = 0,
    this.isOnline = false,
    required this.createdAt,
    this.lastActiveAt,
  });

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['id'],
      username: json['username'],
      email: json['email'],
      phone: json['phone'],
      avatar: json['avatar'],
      location: json['location'],
      bio: json['bio'],
      skills: (json['skills'] as List<dynamic>?)
              ?.map((skill) => SkillTag.fromJson(skill))
              .toList() ??
          [],
      certifications: (json['certifications'] as List<dynamic>?)
              ?.map((cert) => Certification.fromJson(cert))
              .toList() ??
          [],
      rating: (json['rating'] as num?)?.toDouble() ?? 0.0,
      reviewCount: json['review_count'] ?? 0,
      followersCount: json['followers_count'] ?? 0,
      followingCount: json['following_count'] ?? 0,
      isOnline: json['is_online'] ?? false,
      createdAt: DateTime.parse(json['created_at']),
      lastActiveAt: json['last_active_at'] != null
          ? DateTime.parse(json['last_active_at'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'email': email,
      'phone': phone,
      'avatar': avatar,
      'location': location,
      'bio': bio,
      'skills': skills.map((skill) => skill.toJson()).toList(),
      'certifications': certifications.map((cert) => cert.toJson()).toList(),
      'rating': rating,
      'review_count': reviewCount,
      'followers_count': followersCount,
      'following_count': followingCount,
      'is_online': isOnline,
      'created_at': createdAt.toIso8601String(),
      'last_active_at': lastActiveAt?.toIso8601String(),
    };
  }

  UserModel copyWith({
    String? id,
    String? username,
    String? email,
    String? phone,
    String? avatar,
    String? location,
    String? bio,
    List<SkillTag>? skills,
    List<Certification>? certifications,
    double? rating,
    int? reviewCount,
    int? followersCount,
    int? followingCount,
    bool? isOnline,
    DateTime? createdAt,
    DateTime? lastActiveAt,
  }) {
    return UserModel(
      id: id ?? this.id,
      username: username ?? this.username,
      email: email ?? this.email,
      phone: phone ?? this.phone,
      avatar: avatar ?? this.avatar,
      location: location ?? this.location,
      bio: bio ?? this.bio,
      skills: skills ?? this.skills,
      certifications: certifications ?? this.certifications,
      rating: rating ?? this.rating,
      reviewCount: reviewCount ?? this.reviewCount,
      followersCount: followersCount ?? this.followersCount,
      followingCount: followingCount ?? this.followingCount,
      isOnline: isOnline ?? this.isOnline,
      createdAt: createdAt ?? this.createdAt,
      lastActiveAt: lastActiveAt ?? this.lastActiveAt,
    );
  }
}

class SkillTag {
  final String id;
  final String name;
  final String category;
  final String? description;
  final SkillLevel level;
  final bool isCertified;
  final DateTime createdAt;

  SkillTag({
    required this.id,
    required this.name,
    required this.category,
    this.description,
    required this.level,
    this.isCertified = false,
    required this.createdAt,
  });

  factory SkillTag.fromJson(Map<String, dynamic> json) {
    return SkillTag(
      id: json['id'],
      name: json['name'],
      category: json['category'],
      description: json['description'],
      level: SkillLevel.values.firstWhere(
        (e) => e.toString() == 'SkillLevel.${json['level']}',
        orElse: () => SkillLevel.beginner,
      ),
      isCertified: json['is_certified'] ?? false,
      createdAt: DateTime.parse(json['created_at']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'category': category,
      'description': description,
      'level': level.toString().split('.').last,
      'is_certified': isCertified,
      'created_at': createdAt.toIso8601String(),
    };
  }
}

enum SkillLevel {
  beginner, // 初级
  intermediate, // 中级
  advanced, // 高级
  expert, // 专家
}

class Certification {
  final String id;
  final String type;
  final String title;
  final String? description;
  final CertificationStatus status;
  final DateTime? verifiedAt;
  final DateTime createdAt;

  Certification({
    required this.id,
    required this.type,
    required this.title,
    this.description,
    required this.status,
    this.verifiedAt,
    required this.createdAt,
  });

  factory Certification.fromJson(Map<String, dynamic> json) {
    return Certification(
      id: json['id'],
      type: json['type'],
      title: json['title'],
      description: json['description'],
      status: CertificationStatus.values.firstWhere(
        (e) => e.toString() == 'CertificationStatus.${json['status']}',
        orElse: () => CertificationStatus.pending,
      ),
      verifiedAt: json['verified_at'] != null
          ? DateTime.parse(json['verified_at'])
          : null,
      createdAt: DateTime.parse(json['created_at']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'type': type,
      'title': title,
      'description': description,
      'status': status.toString().split('.').last,
      'verified_at': verifiedAt?.toIso8601String(),
      'created_at': createdAt.toIso8601String(),
    };
  }
}

enum CertificationStatus {
  pending, // 待审核
  approved, // 已通过
  rejected, // 已拒绝
  expired, // 已过期
}
